package com.shop.service.implement;

import java.util.List;

import com.shop.dao.IUserDao;
import com.shop.pojo.User;
import com.shop.service.IUserService;
import com.shop.util.MD5;
import com.shop.util.Pager;

public class UserService implements IUserService {
	private IUserDao userDao;
	
	public User login(String userid, String password){
		User user= findUserByUserId(userid);
		if(user==null)return null;
		if(new MD5().getMD5ofStr(password).equalsIgnoreCase(user.getPassword())){
			return user;
		}
		else return null;
	}
	public Pager<User> findByLatestOrder(int pageSize,int  pageNo){
		int startIndex=pageSize*(pageNo-1);
		int count=userDao.count();
		List<User> list=(List<User>)userDao.findByLatestOrder(startIndex, pageSize);
		return new Pager<User>(pageSize,pageNo,count,list);
	}
	public void addUser(User user){
		userDao.save(user);
	}
	public void update(User user){
		userDao.update(user);
	}
	public User findUserByUserId(String userid){
		return (User)userDao.findByUserId(userid);
	}
	public User findUser(String id){
		return (User)userDao.findById(id);
	}
	public Pager<User> findAll(int pageSize,int pageNo){
		int startIndex=pageSize*(pageNo-1);
		int count=userDao.count();
		List<User> list=(List<User>)userDao.findAll(startIndex, pageSize);
		return new Pager<User>(pageSize,pageNo,count,list);
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	public void delete(String id) {
		userDao.deleteById(id);
	} 

}
